<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\controller\adminapi\platform\free;

use app\controller\adminapi\platform\PlatformController;
use app\common\model\user\UserWallet;
use think\App;
use think\Request;
use app\common\model\platform\Platform;
use hiduoke\services\FormBuilder as Form;
use app\common\controller\AdminController;
use app\common\model\platform\PlatformWechat;

/**
 * Class Other 独立平台
 * @package app\controller\adminapi\platform
 * User: liuhongbin
 * Date: 2021-05-25 14:14
 */
class Free extends PlatformController
{

    /**
     * 显示微信平台列表
     * @Route("wechat", method="GET")
     * @return \think\Response
     */
    public function index()
    {
        //
        return $this->success("ok");
    }

    /**
     * 显示创建微信平台表单页.
     * @Route("wechat/create", method="GET")
     * @return \think\Response
     */
    public function create()
    {
        $rule = array();
        //创建表单
        $form = Form::createForm("/adminapi/platform/free/index")->setMethod("POST");
        //新增表单
        $rule[] = Form::input('sort', '排序', "0");
        $rule[] = Form::input('name', '平台名称', "")->required();
        $rule[] = Form::input('description', '描述', "");
        $rule[] = Form::dkImage('logo', '图标', "");
        $form->setTitle("新增微信公众平台")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存新建的微信平台
     * @Route("wechat", method="POST")
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $wallet = $this->userInfo['wallet'];
        if ($wallet['free'] <= $wallet['use_count']['free']) {
            return $this->error("创建次数不足");
        }
        $formData = $request->post('formData', []);
        $platform = new Platform();
        $platform_data = array(
            "type"        => 5,
            "uid"         => $this->userInfo['uid'],
            "name"        => $formData['name'],
            "description" => $formData['description'],
            "logo"        => $formData['logo'],
            "sort"        => $formData['sort'],
        );
        $platform->save($platform_data);
        return $this->success("新增成功");
    }

    /**
     * 显示指定的微信平台
     * @Route("wechat/id", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑微信平台表单页.
     * @Route("wechat/id/edit", method="GET")
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $rule = array();
        //创建表单
        $platform = new Platform();
        $row = $platform->with("wechat")->where(['plaid' => $id])->find();
        if (!$row) {
            return $this->error("当前平台不存在");
        }
        $form = Form::createForm("/adminapi/platform/free/index/" . $id)->setMethod("PUT");
        //新增表单
        $rule[] = Form::input('sort', '排序', $row->sort);
        $rule[] = Form::input('name', '平台名称', $row->name)->required();
        $rule[] = Form::input('description', '描述', $row->description);
        $rule[] = Form::dkImage('logo', '平台图标', $row->logo)->appendToBody();
        $form->setTitle("平台设置")->setRule($rule);
        return $this->success("", Form::getFormData($form));
    }

    /**
     * 保存更新的微信平台
     * @Route("wechat/id", method="PUT")
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $formData = $request->post('formData', []);
        $platform = new Platform();

        $platform_data = $platform->where(['plaid' => $id])->find();
        if (!$platform_data) {
            return $this->error("当前平台不存在");
        }
        $platform_data->save([
            "type"        => 5,
            "name"        => $formData['name'],
            "description" => $formData['description'],
            "logo"        => $formData['logo'],
            "sort"        => $formData['sort'],
        ]);
        return $this->success("编辑成功");
    }

    /**
     * 删除指定微信平台
     * @Route("wechat/id", method="DELETE")
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

}
